var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var cpage = 1;
var screnn_w = 320;
var uid = 0;
var hasMore = true;

var buy_hasMore = true;
var buy_loadingMore = false;
Page({  
  data: {  
    colorSet: '',
    isIphone_x: false,
    goodTypeView_w: 100,
    rightStyleClass:"",
    leftStyleClass:"",
    contentData:0,

    tc: 0,
    inviteCount: 0,//总邀请人数
    recents:[],

    buy_orders: [],
    hasBuyData:true,
    uid:0,
    token:'',
    openid:'',

    myrecord:[],
   
  },  
   
  onLoad: function (options) {  
    wx.showLoading({
      title: '加载中',
      mask:'true'
    })

    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    var tc = app.get_tc();
    that.setData({
      tc: tc
    })

   

    //获取绑定orgid信息
    app.GetUserInfo(util.orgid, function (res) {
      that.setData({
        org_name: res.uname
      })
    });

    var inviteuid = options.inviteuid;
    //调用应用实例的方法获取全局数据
    app.LoginUserInfo(function(res){
      //更新数据
      that.setData({
        userInfo:res
      })
      uid = res.uid;
    }, inviteuid);
    hasMore = true;
    cpage = 1;

    wx.getSystemInfo({
      success: function (res) {
    
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          screnn_w : res.windowWidth,
          content_w: res.windowWidth - 78,
          goodTypeView_w: (res.windowWidth - 78) / 3
        });
        
      }
    })
    //获取颜色配置
    app.getColorSetData(function (color) {
      var col = color.goods_col ? parseInt(color.goods_col) : 3;
      col = col > 0 ? col : 3;
      that.setData({
        colorSet: color,
        content_item_w: (screnn_w - (col + 1) * 5) / col,
        goodTypeView_w: screnn_w / 3,
      })
    });



    

    //当前登录用户信息
    app.LoginUserInfo(function (res) {
      var authFail = res == -1;//用户拒绝授权
      if (authFail) {
        wx.hideLoading();
        app.reAuthorize(that.reGetUserInfo);
        return;
      }
      //更新数据
      that.setData({
        uid: res.uid,
        token: res.token,
        openid: res.wxsmall_openid
      });
    });

    that.getWinningRecord();

    wx.hideLoading();
    
  },  

  onReady:function(){
    // 页面渲染完成    
    var that = this;
    //获取邀请人数
    that.loadInviteCount();
    //请求当前用户的动态    
    that.loadInviteList(cpage);
  },

  //点击助力好友
  getHelpFriend:function(){
    var that = this;
    var bgcolor = that.data.colorSet.btnbg;
    var style = "color:#fff;background-color:"+bgcolor+";";
    that.setData({
      leftStyleClass:style,
      rightStyleClass:'',
      contentData:0,
    });
  },


  //点击中奖记录
  getWinningRecord:function(){
    
    var that = this;
    var bgcolor = that.data.colorSet.btnbg;
    var style = "color:#fff;background-color:"+bgcolor+";";
    that.setData({
      leftStyleClass:'',
      rightStyleClass:style,
      contentData:1,
      buy_orders:[] //每次点击先清空上次数据
    });

    // that.getZongOrderList(2, 1, 20, false);
    that.getmyrecord();
  },

  getmyrecord:function(){
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getmyrecord",
        "appid": util.appid,
        "uid": that.data.uid,

      },
      success: function (responseData) {
        console.log("getmyrecord==",responseData);
        that.setData({
          myrecord:responseData.data
        })
      },
    });
  },

  //请求邀请总人数
  loadInviteCount: function () {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "invitecount",
        "uid": uid
      },
      success: function (responseData) {
        console.log(responseData);
        that.setData({
          inviteCount: responseData.data.count ? responseData.data.count : 0
        });
      }
    });
  },

  //请求邀请列表数据
  loadInviteList: function(cpage){
    var that = this;
    util.api({
      data:{
        "publickey":util.publiccode,
        "ac":"inviteinfo",
        "uid":uid,
        "page":cpage
      },
      success:function(responseData){
        console.log(responseData);
        if (responseData.res == false) {
          that.setData({
            hasData: false,
            recents: []
          });
        } else {
          var data = responseData.data;
          if(data && data.length > 0) {
                that.setData({
                  hasData:true,
                    recents:that.data.recents.concat(data)
                });
          }
          
        }
      }
    });
  },

  //加载更多
  loadMore:function(){
      if(hasMore){
        cpage ++;
        this.loadInviteList(cpage);
      }
  },

  //获取订单 列表  type: 1 卖出  2:买入
  // * page: 
  // refresh 是否是刷新
  getZongOrderList: function (orderType, page, num, refresh) {
    var that = this;
    var params = {
      "ac": "getzongorder",
      "publickey": util.publiccode,
      "type": orderType,
      "page": page,
      "num": num
    };
    if (!num) {
      num = 20;
    }
    if (orderType == 2) {//买入
      params.uid = that.data.uid;
    }
    console.log("orders params:", params);
    util.api({
      data: params,
      success: function (responseData) {
        console.log(responseData);
        if (!responseData.data.msg) {
          console.log(responseData.data);
          //买入

          var result = responseData.data;
          
  
          if (!refresh && result.length > 0) {
            result = that.data.buy_orders.concat(responseData.data);
          }
          var res = [];
          for(var i =0;i<result.length;i++){
           
            if(result[i]['isdraw'] == 1){
              res.push(result[i]);
            }
          }

      
          that.setData({
            buy_orders: res
          });

        } else {
          //买入
          buy_hasMore = false;
          if (that.data.buy_orders.length <= 0) {
            that.setData({
              hasBuyData: false
            });
          }

        }
        //买入
        buy_loadingMore = false;

        wx.hideLoading();
      }
    });
  },
  //奖品提示
  PrizePrompt:function(){
    wx.showModal({
      title: '提示',
      content: '奖品请到微信卡包中查看',
      showCancel: false,
    })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

  },
})
